import java.util.Map;

import com.speljohan.rsbot.script.Constants;
import com.speljohan.rsbot.script.Script;
import com.speljohan.rsbot.script.wrappers.RSInterface;
import com.speljohan.rsbot.script.wrappers.RSTile;

public class TBoltEnchanter extends Script {

	int interfaceID = 0;

	public TBoltEnchanter() {
		setParameter("author", "!@!@!");
		setParameter("version", 1.0);
		setParameter("name", "TBoltEnchanter");
		setParameter("category", "Magic");
		setParameter("description", desc());
	}

	public String desc() {
		String s = "";
		s += "<html><body><center>";
		s += "<br><br><br><br>";
		s += "<font size=8><b>" + getName() + "</b></font><br>Version: "
				+ getVersion();
		s += "<br><br><br>Select your bolt type:<br>";
		s += "<select name=\"boltType\">";
		s += "<option>Opal";
		s += "<option>Sapphire";
		s += "<option>Jade";
		s += "<option>Pearl";
		s += "<option>Emerald";
		s += "<option>Red Topaz";
		s += "<option>Ruby";
		s += "<option>Diamond";
		s += "<option>Dragonstone";
		s += "<option>Onyx";
		s += "</center></body></html>";
		return s;
	}

	public boolean invContainsBolts() {
		final int[] items = getInventoryArray();
		final int[] boltIDs = new int[] { 9336, 879, 1607, 880, 9339, 9340,
				9341, 9342, 9338 /* MISSING JADE BOLT ID */};
		for (final int item : items) {
			for (final int id : boltIDs) {
				if (item == id) {
					return true;
				}
			}
		}
		return false;
	}

	@Override
	public int loop() {
		if (getMyPlayer().isInCombat()) {
			runAway();
			return random(1000, 2000);
		}
		if (!invContainsBolts()) {
			log("no bolts z0mg");
			stopAllScripts();
		}
		if (RSInterface.getChildInterface(432, interfaceID).getAbsoluteX() > 25) {
			atInterface(RSInterface.getChildInterface(432, interfaceID),
					"Enchant");
			return random(500, 700);
		}
		if (getMyPlayer().getAnimation() != -1) {
			return random(100, 200);
		}
		if (getCurrentTab() != Constants.TAB_MAGIC) {
			openTab(Constants.TAB_MAGIC);
			return random(500, 700);
		}
		clickMouse((643 + random(-4, 4)), (229 + random(-4, 4)), true);
		return random(500, 900);
	}

	@Override
	public void onFinish() {
		log("Thanks for using TBoltEnchanter. :)");
	}

	@Override
	public boolean onStart(final Map<String, String> args) {
		final String type = args.get("boltType");
		if (type.equals("Opal")) {
			interfaceID = 14;
		} else if (type.equals("Sapphire")) {
			interfaceID = 31;
		} else if (type.equals("Jade")) {
			interfaceID = 21;
		} else if (type.equals("Pearl")) {
			interfaceID = 25;
		} else if (type.equals("Emerald")) {
			interfaceID = 34;
		} else if (type.equals("Red Topaz")) {
			interfaceID = 28;
		} else if (type.equals("Ruby")) {
			interfaceID = 37;
		} else if (type.equals("Diamond")) {
			interfaceID = 40;
		} else if (type.equals("Dragonstone")) {
			interfaceID = 43;
		} else if (type.equals("Onyx")) {
			interfaceID = 46;
		}
		return true;
	}

	public void runAway() {
		RSTile foo, bar, bar2;
		foo = getMyPlayer().getLocation();
		setRun(true);

		bar = new RSTile(foo.getX() + random(8, 10), foo.getY() + random(8, 10));
		bar2 = new RSTile(foo.getX() + random(-8, -10), foo.getY()
				+ random(-8, -10));
		if (bar.isValid()) {
			walkTileMM(bar);
		} else if (bar2.isValid()) {
			walkTileMM(bar2);
		}
		wait(random(900, 1000));
		while (getMyPlayer().isMoving()) {
			wait(random(100, 300));
		}
		wait(random(1000, 2000));
		walkTileMM(foo);
	}

}